package com.fh.shop.api.cart.controller;

import com.alibaba.fastjson.JSONObject;
import com.fh.shop.api.annotation.NeedLogin;
import com.fh.shop.api.cart.biz.ICartService;
import com.fh.shop.api.cart.vo.CartVo;
import com.fh.shop.api.common.ServerResponse;
import com.fh.shop.api.common.SystemConst;
import com.fh.shop.api.member.vo.MemberVo;
import com.fh.shop.api.util.KeyUtil;
import com.fh.shop.api.util.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.soap.Addressing;

@RestController
@RequestMapping("/api/carts")
@Api(tags = "购物车接口")
public class CartController {
    @Autowired
    private HttpServletRequest request;
    @Resource(name = "cartService")
    private ICartService cartService;

    @PostMapping
    @NeedLogin
    @ApiOperation("将商品添加到购物车")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "productId", value = "商品id", dataType = "java.lang.Long", required = true, example = "0"),
            @ApiImplicitParam(name = "count", value = "商品数量", dataType = "java.lang.Long", required = true, example = "0"),
            @ApiImplicitParam(name = "x-auth", value = "头信息", dataType = "java.lang.String", required = true, paramType = "header")
    })
    public ServerResponse addItem(Long productId, Long count) {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long memberId = memberVo.getId();
        return cartService.addCartItem(productId, count, memberId);
    }

    @GetMapping
    @NeedLogin
    @ApiOperation("显示购物车数据")
    @ApiImplicitParam(name = "x-auth", value = "头信息", dataType = "java.lang.String", required = true, paramType = "header")
    public ServerResponse findCart() {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long memberId = memberVo.getId();
//        String cartJson = RedisUtil.get(KeyUtil.buildCartKey(memberId));
        String cartJson = RedisUtil.hget(KeyUtil.buildCartKey(memberId), SystemConst.CART_CONENT);
        CartVo cartVo = JSONObject.parseObject(cartJson, CartVo.class);
        return ServerResponse.success(cartVo);
    }

    @DeleteMapping("{id}")
    @NeedLogin
    @ApiOperation("删除购物车中的商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "x-auth", value = "头信息", dataType = "java.lang.String", required = true, paramType = "header"),
            @ApiImplicitParam(name = "id", value = "商品id", dataType = "java.lang.Long", required = true)
    })
    public ServerResponse delete(@PathVariable Long id) {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long memberId = memberVo.getId();
        return cartService.deleteById(id, memberId);
    }

    @DeleteMapping
    @NeedLogin
    @ApiOperation("批量删除购物车中的商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "x-auth", value = "头信息", dataType = "java.lang.String", required = true, paramType = "header"),
            @ApiImplicitParam(name = "ids", value = "商品ids,如 1,5,6", dataType = "java.lang.String", required = true)
    })
    public ServerResponse deleteBatch(String ids) {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long memberId = memberVo.getId();
        return cartService.deleteBatch(ids, memberId);
    }

    @GetMapping("/findCartItemCount")
    @NeedLogin
    @ApiOperation("获取购物车中商品的数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "x-auth", value = "头信息", dataType = "java.lang.String", required = true, paramType = "header")
    })
    public ServerResponse findCartItemCount() {
        MemberVo memberVo = (MemberVo) request.getAttribute(SystemConst.CURR_MEMBER);
        Long memberId = memberVo.getId();
        return cartService.findCartItemCount(memberId);
    }



}
